package m.jclecai.com.activity;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import m.jclecai.com.MyApplication;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.reconmend.RecommentDetail;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.data.Lecai_SDK;
import m.jclecai.com.data.ReceivedData;
import m.jclecai.com.utils.MyLog;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.http.HttpManager;

/**
 * Created by ZD on 2017/7/19.
 * 解说详情
 */

public class RmDetailActivity extends BaseActivity {

    @BindView(R.id.wv_view)
    WebView wvView;
    @BindView(R.id.iv_icon)
    ImageView iv_icon;
    @BindView(R.id.ll_info)
    LinearLayout ll_info;
    @BindView(R.id.iv_level)
    ImageView iv_level;
    @BindView(R.id.iv_type)
    ImageView iv_type;
    @BindView(R.id.tv_nick)
    TextView tv_nick;
    @BindView(R.id.tv_abstract)
    TextView tv_abstract;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_name_type)
    TextView tv_name_type;
    @BindView(R.id.tv_status)
    TextView tv_status;
    @BindView(R.id.tv_add_time)
    TextView tv_add_time;
    @BindView(R.id.tv_start_time)
    TextView tv_start_time;


    @BindView(R.id.tv_gamename)
    TextView tv_gamename;
    @BindView(R.id.tv_name_one)
    TextView tv_name_one;
    @BindView(R.id.tv_time)
    TextView tv_time;
    @BindView(R.id.tv_name_two)
    TextView tv_name_two;
    @BindView(R.id.iv_one)
    ImageView iv_one;
    @BindView(R.id.iv_two)
    ImageView iv_two;
    /**
     * 解说详情相关
     */
    private ReceivedData.RecommentDetailData recommentDetailData = null;
    private ReceivedData.ReconmentPersonDetail reconmentPersonDetail = null;



    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 12345678:
                    ll_info.setVisibility(View.VISIBLE);
                    initValues(recommentDetailData);
                    break;

                case 12345679:
                    bindValues(reconmentPersonDetail);
                    break;
            }
        }
    };
    @Override
    protected int getContentView() {
        return R.layout.activity_rm_detail;
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("解说详情");
        leftOptionText("");
        ll_info.setVisibility(View.INVISIBLE);
        sendReq(Constant_Url.APP_RECOMENTDETAILS_TAG);
        sendReq(Constant_Url.APP_PERSON_DETAIL_TAG);
        wvView.getSettings().setJavaScriptEnabled(true);

    }


    private String url= "";
    /**
     * 解说详情 请求
     */
    private void sendReq(final int tag) {
        Constant_Url.HTTP_URL = MyApplication.HTTP_URL_RECOMMEND;
        Map<String, String> pair = new HashMap<>();
        if (tag == Constant_Url.APP_RECOMENTDETAILS_TAG){
            url = Constant_Url.APP_RECOMENTDETAILS_URL;
            pair.put("user", PreferenceUtil.getUserName(RmDetailActivity.this));//Yes	string	用户名
            pair.put("password", PreferenceUtil.getPassword(RmDetailActivity.this));//Yes	string	密码
            pair.put("id", Lecai_SDK.id);
        }else if (tag == Constant_Url.APP_PERSON_DETAIL_TAG){
            url = Constant_Url.APP_PERSON_DETAIL_URL;
            pair.put("id",Lecai_SDK.persion_id);
        }

        HttpManager.postAsyn(url, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                MyLog.i(request.toString());
                DisPlay("网络异常！！！");
            }

            @Override
            public void onResponse(String response) {
                closeLoadingLayout();
                Gson gson = new Gson();
                if (!TextUtils.isEmpty(response)){
                    Message msg = null;
                    if (tag == Constant_Url.APP_RECOMENTDETAILS_TAG){
                        recommentDetailData =
                                gson.fromJson(response, ReceivedData.RecommentDetailData.class);
                        msg = new Message();
                        msg.what = 12345678;
                    }else if (tag == Constant_Url.APP_PERSON_DETAIL_TAG){
                        reconmentPersonDetail =
                                gson.fromJson(response, ReceivedData.ReconmentPersonDetail.class);
                        msg = new Message();
                        msg.what = 12345679;
                    }
                    handler.sendMessage(msg);
                }
            }
        }, pair);
    }


    RecommentDetail detail = null;
    /**
     *    绑定数据
     */
    private void initValues(ReceivedData.RecommentDetailData recommentDetailData){

        if (Lecai_SDK.from.equals("recomentdetail")){
            ReceivedData.ReconmentPersonDetail reconmentPersonDetail
                    = Lecai_SDK.reconmentPersonDetail;
            bindValues(reconmentPersonDetail);
        }
        tv_title.setText(recommentDetailData.title);
        tv_name_type.setText(recommentDetailData.type);
        tv_status.setText(recommentDetailData.type);
        tv_add_time.setText("发布时间:"+recommentDetailData.addtime);
        tv_start_time.setText("开赛时间:"+recommentDetailData.addtime);
        wvView.loadDataWithBaseURL("", getHtmlData(recommentDetailData.content), "text/html; charset=utf-8", "utf-8", "");
        List<RecommentDetail> teams = recommentDetailData.teams;
        if (null != teams && teams.size()>0&&!teams.equals("[]")){
            detail = teams.get(0);
            Glide.with(RmDetailActivity.this).
                    load(detail.getHomeLogo()).
                    asBitmap().
                    placeholder(R.drawable.my_head_de).
                    into(iv_one);
            Glide.with(RmDetailActivity.this).
                    load(detail.getGuestLogo()).
                    asBitmap().
                    placeholder(R.drawable.my_head_de).
                    into(iv_two);

            tv_gamename.setText(detail.gameName);
            tv_time.setText(detail.getMatchtime());
            tv_name_one.setText(detail.getHomeName());
            tv_name_two.setText(detail.getGuestName());

        }

    }


    private void bindValues(ReceivedData.ReconmentPersonDetail reconmentPersonDetail){
        Glide.with(RmDetailActivity.this).
                load(reconmentPersonDetail.user_face).
                asBitmap().
                placeholder(R.drawable.my_head_de).
                into(iv_icon);
        tv_nick.setText(reconmentPersonDetail.username);
        int jtype = 0;
        iv_icon.setImageResource(jtype == 0 ? R.mipmap.home_living_football : R.mipmap.home_living_basketball);
        iv_level.setImageResource(R.mipmap.famous_zqmj);
        tv_abstract.setText(reconmentPersonDetail.content+"\n");
    }

    private String getHtmlData(String bodyHTML) {
        String head = "<head>" +
                "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> " +
                "<style>img{max-width: 100%; width:auto; height:auto;}</style>" +
                "</head>";
        return "<html>" + head + "<body>" + bodyHTML + "</body></html>";
    }

    String word = "029 美职联 FC达拉斯vs多伦多FC<br />\n北京时间明早美职联赛继续多场赛事较量，FC达拉斯将会迎来多伦多FC的挑战。看主队能否借助主场优势，拿下比赛胜利。<br />\n主场出击的FC达拉斯是目前西部排名第二位的球队，6胜7平3负的战绩，平局偏多。本场比赛，球队将会缺少中后卫沃克森马曼，因为膝盖韧带撕裂的原因。此外，后卫马特希斯以及中场的G科斯塔都因为美洲金杯赛，受到各自国家队的召唤而缺席比赛。<br />\n球队目前已经3场不胜，过去5场赛事也仅仅取胜1场，当中有3场平局，就如他们的总成绩所显示，平局率极高的一支队伍。不过，球队在美职联赛主场对阵多伦多FC，过去8场全部保持不败，当中有6场获胜，这个优势相对较大。<br />\n客军多伦多FC目前是东部排名榜首的球队，上轮击败新英伦革命之后，球队连续2场获胜，过去5场比赛有4场不败，状态比主队要好。球队阵中伤兵同样集中在中后场，而前场有几名球员因为国家队任务缺席。<br />\n两队阵容情况相似，但客军近态更好。不过往绩交锋，FC达拉斯在主场也是颇具优势，这样一来，欧指主胜不退下调以及亚盘维持主让半球让步，即使高水位赔付，也是对主队的一众倾向，毕竟他们近态交叉，本场获得支持，主胜值得期待。<span style='color:#E53333;'>参考比分：2-1、3-1。</span><br />\n<span style='color:#E53333;'>竞彩推荐：主胜</span><br />\n<span style='color:#E53333;'>附加推荐：大球（2.5球）、亚盘主胜（半球）</span><br />\n<br />\n搭配一场巴西甲推荐，可作串关选择<br />\n026 巴西甲 帕尔梅拉斯vs格雷米奥<br />\n<span style='color:#E53333;'>竞彩推荐：主胜</span><br />\n<span style='color:#E53333;'>附加推荐：亚盘主胜（半球）</span><br />\n<br />\n<span style='background-color:#FFE500;color:#E53333;'><strong>小妹大波专栏</strong></span><br />\n<span style='background-color:#FFE500;color:#E53333;'><strong>22:00 瑞典超 马尔默vsAFC联</strong></span><br />\n<span style='background-color:#FFE500;color:#E53333;'><strong>推荐：大球（3/3.5球）</strong></span><br />\n<span style='background-color:#FFE500;color:#E53333;'><strong>02:15 挪超 松达尔vs奥勒松</strong></span><br />\n<span style='background-color:#FFE500;color:#E53333;'><strong>推荐：大球（2.5/3球）</strong></span><br />";
}
